package com.szqhyxj.youxiangju.biz.creditReport.signature;

import com.szqhyxj.youxiangju.biz.core.ImageUploadResult;
import com.szqhyxj.youxiangju.biz.core.ImageUploader;
import com.szqhyxj.youxiangju.biz.creditReport.CreditReportEntity;
import com.szqhyxj.youxiangju.biz.creditReport.CreditReportModel;
import com.szqhyxj.youxiangju.sys.BaseResult;
import com.szqhyxj.youxiangju.sys.util.BitmapCompressFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by leung on 2018/11/4.
 */

public class SignaturePresenter implements SignatureContract.Presenter {
    SignatureContract.View view;
    private List<Disposable> disposables = new ArrayList<>();
    CreditReportEntity creditReportEntitys;
    public SignaturePresenter(SignatureContract.View view) {
        this.view = view;
    }

    @Override
    public void onDestroy() {

    }

    @Override
    public void postSign(File file, final CreditReportEntity creditReportEntity) {
        view.showLoading();
        File tfile = BitmapCompressFile.compressFile(file);

        Disposable disposable = ImageUploader
                .uploadSign(tfile)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        new Consumer<BaseResult<ImageUploadResult>>() {
                            @Override
                            public void accept(BaseResult<ImageUploadResult> uploadResult) {
                                view.dismissLoading();
                                if (uploadResult.businessLogicSuccess()) {
                                  creditReportEntity.setAutographPic(uploadResult.getData().getFileUrl());
                                   view.showCommitSuccess(uploadResult.getData().getFileUrl());
                                    signCommit(creditReportEntity);

                                } else {
                                    view.showFile(uploadResult.getMessage());
                                }
                                tfile.delete();
                            }
                        }, new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) {
                                view.dismissLoading();
                                view.showFile(throwable.getMessage());
                                tfile.delete();
                            }
                        }
                );
        disposables.add(disposable);
    }


    public void signCommit(CreditReportEntity creditReportEntity) {
        view.showLoading();
        CreditReportModel.getReport(creditReportEntity)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult>() {
                    @Override
                    public void accept(BaseResult baseResult) throws Exception {
                        view.dismissLoading();
                        if(baseResult.businessLogicSuccess()){
                            view.reportSuccess(creditReportEntity);
                        }else{
                            view.showFile(baseResult.getMessage());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.dismissLoading();
                        view.showFile(throwable.getMessage());
                    }
                });
    }
}
